import request from '@/router/axios.js';

export default function (url, methodType, data,fileName) {
    let loading = this.$loading({
        text:"正在下载"
    });
    request({
        url: url,
        method: methodType,
        data: data,
        params: methodType.toLowerCase() === 'get' ? data : null,
        responseType: 'blob',
    }).then(res => {
        const blob = new Blob([res.data]);
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download =fileName?"fileName":res.headers['download-filename'];
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
        loading.close()
    }).catch(err => {
        loading.close();
        this.$message.error('下载失败')
    })
}